package com.xrit.sunnyday.controller;

import com.xrit.sunnyday.constant.MessageConstant;
import com.xrit.sunnyday.entity.Result;

import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {

    @GetMapping("/getLoginUsername")
    public Result getLoginUsername(){
        //获取用户登录的认证信息
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        //获取登录的用户名
        String username = user.getUsername();
        System.out.println(username);
        //返回给前端显示
        return new Result(true, MessageConstant.GET_USERNAME_SUCCESS,username);
    }
}
